package com.kuaimacode.kframework.admin.controller.category;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.github.pagehelper.PageInfo;
import com.kuaimacode.kframework.admin.services.category.BuyIconService;
import com.kuaimacode.kframework.admin.services.category.CategoryCfgService;
import com.kuaimacode.kframework.admin.util.SessionUtil;
import com.kuaimacode.kframework.common.Constant;
import com.kuaimacode.kframework.common.JsonResult;
import com.kuaimacode.kframework.constants.PageConstants;
import com.kuaimacode.kframework.mybatis.models.admin.SysAdminUser;
import com.kuaimacode.kframework.mybatis.models.agent.SysAgent;
import com.kuaimacode.kframework.mybatis.models.agent.SysAgentCategoryCfg;
import com.kuaimacode.kframework.mybatis.models.category.BuyIcon;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @author Administrator
 * @date 2019/03/18
 */
/**
 * Controller
 */
@Controller
@RequestMapping({"/categoryCfg"})
public class CategoryCfgController {
	private static String PAGE_FILE_NAME = "categoryCfg/";

	@Autowired
	private CategoryCfgService categoryCfgServices;

	@Autowired
	private BuyIconService buyIconService;
	//查询平台默认品类设置
	@RequestMapping(value = {"/index"})
	public String index(@RequestParam(value = "pageNum", required = true, defaultValue = PageConstants.PAGE_NUM+"") int pageNum,
										@RequestParam(value = "pageSize", required = true, defaultValue = PageConstants.PAGE_SIZE+"") int pageSize,
						SysAgentCategoryCfg categoryCfg,ModelMap model) {
		/*PageInfo<SysAgentCategoryCfg> pageInfo = categoryCfgServices.findAll(pageNum, pageSize,categoryCfg);
		// pageInfo.list categoryCfg
		model.put("pageInfo", pageInfo);
		model.put("categorySearch",categoryCfg);*/
		return PAGE_FILE_NAME + "index";
	}

	//查询平台默认品类设置
	@RequestMapping(value = {"/index_category/{type}"})
	public String categoryIndex(@RequestParam(value = "pageNum", required = true, defaultValue = PageConstants.PAGE_NUM+"") int pageNum,
						@RequestParam(value = "pageSize", required = true, defaultValue = PageConstants.PAGE_SIZE+"") int pageSize,
								@PathVariable int type,ModelMap model) {
		SysAgentCategoryCfg categoryCfg = new SysAgentCategoryCfg();
		categoryCfg.setType(type);
		PageInfo<SysAgentCategoryCfg> pageInfo = categoryCfgServices.findAll(pageNum, pageSize,categoryCfg);
		// pageInfo.list categoryCfg
		model.put("pageInfo", pageInfo);
		model.put("categorySearch",categoryCfg);
		model.put("title",type==1?"帮我送":(type==2?"帮我取":"随意购"));
		return PAGE_FILE_NAME + "index_category";
	}



	@RequestMapping(value = "/add/{type}", method = RequestMethod.GET)
	public String add(@PathVariable int type,ModelMap map) {
		SysAgentCategoryCfg categoryCfg = new SysAgentCategoryCfg();
		categoryCfg.setInitiateKm(new BigDecimal(3));
		categoryCfg.setExceedKm(new BigDecimal(10.00));
		categoryCfg.setInitiatePrice(new BigDecimal(6.00));
		categoryCfg.setOverInitKmPrice(new BigDecimal(2.00));
		categoryCfg.setExceedExtraPrice(new BigDecimal(0.5));

		categoryCfg.setWeightBegin(3);
		categoryCfg.setWeightEnd(25);
		categoryCfg.setWeightExtraPrice(new BigDecimal(0.5));

		categoryCfg.setParcelInsuBegin(new BigDecimal(0));
		categoryCfg.setParcelInsuEnd(new BigDecimal(10000));
		categoryCfg.setParcelInsuRate(new BigDecimal(5));

		categoryCfg.setDrawRate(new BigDecimal(70));//师傅提成比例
		categoryCfg.setType(type);
		//查询随意购图标
		List<BuyIcon> buyIcons = buyIconService.findAll();

		map.put("categoryCfg", categoryCfg);
		map.put("isAdd", true);
		map.put("icons",buyIcons);
		return PAGE_FILE_NAME+"form";
	}


	@RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
	public String edit(@PathVariable String id, ModelMap map) {
		SysAgentCategoryCfg categoryCfg = categoryCfgServices.getById(id);
		//查询随意购图标
		List<BuyIcon> buyIcons = buyIconService.findAll();
		map.put("icons",buyIcons);
		map.put("categoryCfg", categoryCfg);
		map.put("isAdd", false);
		return PAGE_FILE_NAME+"form";
	}

	@RequestMapping(value = {"/edit"}, method = RequestMethod.POST)
	@ResponseBody
	public JsonResult edit(SysAgentCategoryCfg categoryCfg, Boolean isAdd,ModelMap map) {
		try {

			categoryCfg.setUpdateBy(SessionUtil.getUserId());
			categoryCfg.setUpdateAt(new Date());
			//查询图标信息
			BuyIcon buyIcon = buyIconService.getById(categoryCfg.getIcon());
			if(buyIcon!=null){
				categoryCfg.setIcon(buyIcon.getIcon());
				categoryCfg.setIconPre(buyIcon.getPreIcon());
			}

			if(isAdd){
				// 新建保存
				categoryCfg.setCreateBy(SessionUtil.getUserId());
				categoryCfgServices.saveDefaultCategory(categoryCfg);
			}else{
				// 编辑保存
				categoryCfgServices.updateById(categoryCfg);
				SysAgentCategoryCfg sysAgentCategoryCfg = new SysAgentCategoryCfg();
				sysAgentCategoryCfg.setCategory(categoryCfg.getCategory());
				UpdateWrapper updateWrapper = new UpdateWrapper();
				updateWrapper.eq("sys_category_id",categoryCfg.getId());
				categoryCfgServices.update(sysAgentCategoryCfg,updateWrapper);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return JsonResult.failure(e.getMessage());
		}
		return JsonResult.success();
	}


	@RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
	@ResponseBody
	public JsonResult delete(@PathVariable String id, ModelMap map) {
		try {

			categoryCfgServices.deleteCategory(id);
		} catch (Exception e) {
			return JsonResult.failure(e.getMessage());
		}
		return JsonResult.success();
	}

}
